using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Contacts
    {
          #region Common
        public eContacts   Data;

        public Contacts()
        {
            Data = new eContacts();
		}

        ~Contacts()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cContacts objDAL = new cContacts();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cContacts objDAL = new cContacts();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cContacts objInfor = new cContacts(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.ContactID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cContacts objInfor = new cContacts();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cContacts objDAL = new cContacts();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
             
                objDAL.Save();
                nResult = objDAL.ContactID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cContacts objInfor)
        {
            try { objInfor.Fullname = Data.Fullname; }
            catch { objInfor.Fullname = ""; }
            objInfor.Email = Data.Email;
            try { objInfor.Telephone = Data.Telephone; }
            catch { objInfor.Telephone = ""; }
            objInfor.MainContent = Data.MainContent;
            objInfor.CreatedDate = Data.CreatedDate;
            objInfor.Status = Data.Status; 
        }

        private void ConvertData(cContacts objInfor)
        {
            Data.Fullname =objInfor.Fullname  ;
            Data.Email =objInfor.Email ;
            Data.Telephone  =objInfor.Telephone ;
            Data.MainContent =objInfor.MainContent ;
            Data.CreatedDate = objInfor.CreatedDate;
            Data.Status  =objInfor.Status ; 
        }

        public DataTable Search(int ItemView, int Status,int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cContacts objDAL = new cContacts();
            tbResult = objDAL.Search(ItemView, Status, PageIndex, PageSize, ref  PageCount);
            return tbResult;
        }

    
        #endregion
    }
}
